﻿using System;

namespace ComplexNumber
{
    class ComplexNumber
    {
        double realPart;
        double imaginaryPart;

        public double RealPart
        {
            get
            {
                return realPart;
            }
            set
            {
                realPart = value;
            }
        }

        public double ImaginaryPart
        {
            get
            {
                return imaginaryPart;
            }
            set
            {
                imaginaryPart = value;
            }
        }

        public ComplexNumber(double realPart, double imaginaryPart)
        {
            this.realPart = realPart;
            this.imaginaryPart = imaginaryPart;
        }

        public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
        {
            double real = c1.realPart + c2.realPart;
            double complex = c1.imaginaryPart + c2.imaginaryPart;
            ComplexNumber result = new ComplexNumber(real, complex);
            return result;
        }

        public static ComplexNumber operator -(ComplexNumber c1, ComplexNumber c2)
        {
            double real = c1.realPart - c2.realPart;
            double complex = c1.imaginaryPart - c2.imaginaryPart;
            ComplexNumber result = new ComplexNumber(real, complex);
            return result;
        }

        public static ComplexNumber operator *(ComplexNumber c1, ComplexNumber c2)
        {
            double real = c1.realPart * c2.realPart - c1.imaginaryPart * c2.imaginaryPart;
            double complex = c1.imaginaryPart * c2.realPart + c1.realPart * c2.imaginaryPart;
            ComplexNumber result = new ComplexNumber(real, complex);
            return result;
        }

        public static ComplexNumber operator /(ComplexNumber c1, ComplexNumber c2)
        {
            double denom = c2.realPart * c2.realPart + c2.imaginaryPart + c2.imaginaryPart;
            double real = (c1.realPart * c2.realPart + c1.imaginaryPart * c2.imaginaryPart) / denom;
            double complex = (c1.imaginaryPart * c2.realPart - c1.realPart * c2.imaginaryPart) / denom;
            ComplexNumber result = new ComplexNumber(real, complex);
            return result;
        }

        public override string ToString()
        {
            string number = string.Format("{0} + {1}i", realPart, imaginaryPart);
            return number;
        }

        public double ToDouble()
        {
            double result = this.realPart;
            return result;
        }

        public ComplexNumber DoubleToComplex(double number)
        {
            double realPart = number;
            double imaginaryPart = 0;
            ComplexNumber result = new ComplexNumber(realPart, imaginaryPart);
            return result;
        }

        public double this[int index]
        {
            get
            { 
                if (index == 0)
                {
                    return this.realPart;
                }
                if (index == 1)
                {
                    return this.imaginaryPart;
                }
                throw new IndexOutOfRangeException();
            }
            set
            {
                if (index == 0)
                {
                    this.realPart = value;
                }
                if (index == 1)
                {
                    this.imaginaryPart = value;
                }
                throw new IndexOutOfRangeException();
            }
        }
    }
}